package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.mapper.NCCodeAdviceMapper;
import com.foreverwin.jzymes.common.service.INCCodeAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

@Service
public class NCCodeAdviceServiceImpl implements INCCodeAdviceService {

    @Autowired
    NCCodeAdviceMapper ncCodeAdviceMapper;

    /**
     * 检查不合格代码是否存在
     *
     * @param site
     * @param ncCode
     * @return
     */
    public Map<String,Object> existNCCode( String site, String ncCode ){
        String handle = BOHandleUtil.buildNcCodeHandle( site, ncCode );
        Map<String,Object> ncCodeMap = ncCodeAdviceMapper.selectNCCodeMapById( handle );
        Assert.notNull( ncCodeMap, I18nUtil.getI18nText( "common.ncCodeNotExist", new String[]{ ncCode } ) );

        return ncCodeMap;
    }

    /**
     * 查询nc有效操作
     *
     * @param site
     * @param ncCode
     * @return
     */
    public String findNCValidOper( String site, String ncCode ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        Assert.hasText( ncCode, I18nUtil.getI18nText( "common.ncCodeNotBlank" ) );
        String ncCodeDefBo = BOHandleUtil.buildNCCodeDefHandle( site, ncCode );
        Map<String,Object> ncValidOperMap =  ncCodeAdviceMapper.selectNCValidOper( ncCodeDefBo );
        if( ncValidOperMap == null ){
            return null;
        }
        String operationBo = (String) ncValidOperMap.get( "OPERATION_BO" );
        String[] parts = BOHandleUtil.splitOperationHandle( operationBo );
        return parts[1];
    }

    /**
     * 查询站点所有ncCode
     *
     * @param site
     * @return
     */
    @Override
    public List<Map<String, Object>> findNcCodeBySite(String site) {
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        List<Map<String, Object>> list = ncCodeAdviceMapper.selectNcCodeBySite( site );
        Assert.notNull( list, I18nUtil.getI18nText( "common.ncCodeNotExist", new String[]{ site } ) );

        return list;
    }


}
